package com.azier.kapacitor.script.node;

import com.azier.kapacitor.script.arg.LambdaArg;
import com.azier.kapacitor.script.method.As;
import com.azier.kapacitor.script.method.Keep;
import com.azier.kapacitor.script.method.Quiet;
import com.azier.kapacitor.script.method.Tags;
import com.azier.kapacitor.script.node.nodebind.CommonNodeBind;

import java.util.ArrayList;

/**
* 1 * @Author: luoj
* 2 * @Date: 2019/11/8 11:27
* 3
*/

/**
Create an eval node that will evaluate the given transformation function to each data point. A list of expressions may be provided and will be evaluated in the order they are given. The results are available to later expressions.
**/
/**
创建一个评估节点，该评估节点将评估每个数据点的给定转换函数。可以提供表达式列表，并将按给出的顺序对其进行评估。结果可用于以后的表达式。
**/
public class EvalNode extends AbstractNode implements CommonNodeBind {
    private static final String name = "eval";

    public EvalNode(String ...expressions) {
        super(new ArrayList<>(),new ArrayList<>(),name);
        //
        for(String argf : expressions){
            this.args.add(new LambdaArg(argf));
        }
    }

    /**
    List of names for each expression. The expressions are evaluated in order. The result of an expression may be referenced by later expressions via the name provided.
    **/
    /**
    每个表达式的名称列表。表达式按顺序求值。表达式的结果可以由以后的表达式通过提供的名称引用。
    **/
    public EvalNode as(String ...names){
        As as = new As(names);
        super.addMethod(as);
        return this;
    }
    /**
    If called the existing fields will be preserved in addition to the new fields being set. If not called then only new fields are preserved. (Tags are always preserved regardless how is used.)
    **/
    /**
    如果调用，则除了设置新字段外，还将保留现有字段。如果未调用，则仅保留新字段。 （无论如何使用，始终会保留标签。）
    **/
    public EvalNode keep(String ...fields){
        Keep keep = new Keep(fields);
        super.addMethod(keep);
        return this;
    }
    /**
    Suppress all error logging events from this node.
    **/
    /**
    禁止来自此节点的所有错误记录事件。
    **/
    public EvalNode quiet(){
        Quiet quiet = new Quiet();
        super.addMethod(quiet);
        return this;
    }
    /**
    Convert the result of an expression into a tag. The result must be a string. Use the expression function to convert types.
    **/
    /**
    将表达式的结果转换为标签。结果必须是字符串。使用表达式函数转换类型。
    **/
    public EvalNode tags(String ...names){
        Tags tags = new Tags(names);
        super.addMethod(tags);
        return this;
    }
}